﻿"use strict";
define(['admin/app', "service/admin/chargeService", "service/admin/merchantService"], function (app) {
    app.controller('chargeController', function ($scope, $stateParams, $location, $uibModal, $state, utils, chargeService, merchantService) {
        $scope.query = {
            page: 1, size: 20, pay_channel: 0, name: '', mobile: '', start_time: '', end_time: '', merchant_no: '', order_no: '', status: 0, merchant_id: 0, type : 0
        };
        angular.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                chargeService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            search_page:function(page){
            	$scope.query.page = page;
	            $location.search('page', $scope.query.page);
            },
            get_oems: function () {
                merchantService.getoem().success(function(response) {
                    $scope.oems = response.data;
                    $scope.oems.unshift({id:0,name:"全部"});
                });
            },
            get: function () {
                if ($stateParams.id > 0) {
                    chargeService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '详情';
                }
            },
            status_name:function(status){
            	if(status==1)return "待支付";
            	else if(status==2)return "已支付";
            	else if(status==3)return "待确认";
            	else return "失败";
            },
            type_name:function(type){
            	if(type==1)return "收款";
            	else if(type==2)return "升级";
            	else if(type==3)return "商品交易";
            	else if(type==3)return "不分账";
            	else return "代付充值";
            },
            charge_first:function(item){
            	$scope.psdisabled = true;
                chargeService.charge_first({merchant_no:item.merchant.merchant_no,order_no:item.order_no,pay_channel:item.pay_info.pay_channel}).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.search();
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            charge_notice:function(item){
            	$scope.psdisabled = true;
                chargeService.charge_notice({merchant_no:item.merchant.merchant_no,order_no:item.order_no,pay_channel:item.pay_info.pay_channel}).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            goBack: function () {
                history.back();
            }
        }

        angular.extend($scope, methods);
        if ($state.includes('app.charge.detail')) {
            methods.get();
        } else if ($state.includes('app.charge.list')){
        	methods.search();
        	methods.get_oems();
    	}
    });
});
